package com.xuebaopeng.xbp.constants;

import android.app.Activity;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

/**
 * ****************************************
 * Created by xbp on 2016/7/13.
 * 记录app的状态
 * ****************************************
 */
public class AppStatusTracker implements Application.ActivityLifecycleCallbacks {
    private static final long MAX_INTERVAL=5*60*1000;
    private static AppStatusTracker tracker;
    private Application application;
    private int mAppStatus=ConstantValues.STATUS_ONLINE;
    private boolean isForground;//标志位记录app是否在前台执行，true是
    private int activeCount;//计数器记录有多少个activity被创建过
    private long timestamp;
    private boolean isScreenOff;//是否被锁屏过
    private DaemonReceiver receiver;//接受锁屏广播


    private AppStatusTracker(Application application){//注册ActivityLifecycleCallbacks
        this.application=application;
        application.registerActivityLifecycleCallbacks(this);
    }
    public static AppStatusTracker getInstance(){
        return tracker;
    }
    public static void init(Application application){
        tracker=new AppStatusTracker(application);
    }
    public void setAppStatus(int status){
        this.mAppStatus=status;
        if(status==ConstantValues.STATUS_ONLINE){
            if(receiver==null){
                IntentFilter filter=new IntentFilter();
                filter.addAction(Intent.ACTION_SCREEN_OFF);
                receiver=new DaemonReceiver();
                application.registerReceiver(receiver,filter);
            }
        }else if(receiver!=null){
            application.unregisterReceiver(receiver);
            receiver=null;
        }
    }

    public boolean isForground(){
        return isForground;
    }

    private void onScreenOff(boolean isScreenOff){
        this.isScreenOff=isScreenOff;
    }

    public int getAppStatus(){
        return this.mAppStatus;
    }

    /**
     * 检查是否展示解锁图形
     * @return
     */
    public boolean checkIfShowGesture() {
        if (mAppStatus == ConstantValues.STATUS_ONLINE) {
            if (isScreenOff) {
                return true;
            }
            if (timestamp != 0l && System.currentTimeMillis() - timestamp > MAX_INTERVAL) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

    }

    @Override
    public void onActivityStarted(Activity activity) {
        activeCount++;
    }

    @Override
    public void onActivityResumed(Activity activity) {
        isForground=true;
        timestamp=0l;
        isScreenOff=false;
    }

    @Override
    public void onActivityPaused(Activity activity) {
    }

    @Override
    public void onActivityStopped(Activity activity) {
        activeCount--;
        if(activeCount==0){
            isForground=false;
            timestamp=System.currentTimeMillis();
        }
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {

    }

    private class DaemonReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            String action=intent.getAction();
            if (Intent.ACTION_SCREEN_OFF.equals(action)){
                AppStatusTracker.getInstance().onScreenOff(true);
            }

        }
    }
}
